/*
 * @Author: chenhuan 2218981866@qq.com
 * @Date: 2022-11-16 13:27:58
 * @LastEditors: chenhuan 2218981866@qq.com
 * @LastEditTime: 2022-11-19 20:01:59
 * @FilePath: \上课项目练习\111\react-menus-mangener\src\router\menus.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { FunctionComponent } from 'react'
import withRetry from '../components/ErrorBoundary/withRetry'

const AppMain = withRetry(() => import('../views/AppLayout/index'))
const AppFoodCategory = withRetry(() => import('../views/AppFoodCategory/index'))
const AppFoodDetail = withRetry(() => import('../views/AppFoodDetail/index'))
const AppRoomList = withRetry(() => import('../views/AppRoomList/index'))
const AppRoomDesk = withRetry(() => import('../views/AppRoomDesk/index'))
const AppSet = withRetry(() => import('../views/AppSet/index'))
const AppMemberProfile = withRetry(() => import('../views/AppMemberProfile/index'))
const AppMemberPoint = withRetry(() => import('../views/AppMemberPoint/index'))
const AppMemberCard = withRetry(() => import('../views/AppMemberPoint/index'))
const AppPayOff = withRetry(() => import('../views/AppPayOff/index'))
const AppStore = withRetry(() => import('../views/AppStore/index'))

export interface IMeanuItem {
  key: string,
  label: string,
  exact?: string,
  children?: IMeanuItem[],
  component?: FunctionComponent,
}
const menus = [
  {
    key: '/',
    label: '首页',
    exact: 'true',
    component: AppMain,
  },
  {
    key: '/food',
    label: '菜品',
    children: [
      { key: '/food-category', label: '菜品类别', component: AppFoodCategory },
      { key: '/food-detail', label: '菜品档案', component: AppFoodDetail },
    ],
  },
  {
    key: '/room',
    label: '门店',
    children: [
      { key: '/room-list', label: '门店列表', component: AppRoomList },
      { key: '/room-desk', label: '桌位管理', component: AppRoomDesk },
    ],
  },
  {
    key: '/member',
    label: '会员',
    children: [
      { key: '/member-profile', label: '会员档案', component: AppMemberProfile },
      { key: '/member-point', label: '会员积分', component: AppMemberPoint },
      { key: '/member-card', label: '会员储值', component: AppMemberCard },
    ],
  },
  {
    key: '/set',
    label: '设置',
    children: [
      { key: '/set-payoff', label: '支付设置', component: AppPayOff },
      { key: '/set-info', label: '商户资料', component: AppSet },
      { key: '/set-store', label: '微店设置', component: AppStore },
    ],
  },
]
// 推平二维数据
export const flatMenusToRoutes = (items: IMeanuItem[]) => {
  let result: IMeanuItem[] = []
  items.forEach((item) => {
    if (!item.children) result.push({ ...item })
    else result = result.concat(flatMenusToRoutes(item.children))
  })
  return result
}
// // 根据当前的路由路径, 取到menu中的一级key
// export const getMenuKeyByPathname = (pathname: string, items: IMeanuItem[]) => {
//   const result = []
//   for (let i = 0; i < items.length; i++) {
//     if (items[i].children) {
//       for (let j = 0; j < items[i].children!.length; j++) {
//         const element: IMeanuItem = items[i].children![j]
//         if (element.key === pathname) {
//           result.push(items[i].key)
//           break;
//         }
//       }
//     }
//   }
//   return result
// }
export const getMenuKeyByPathname = (pathname: string, items: any) => {
  const result = []
  for (let i = 0; i < items.length; i++) {
    if (items[i].children) {
      for (let j = 0; j < items[i].children.length; j++) {
        const element = items[i].children[j]
        if (element.key === pathname) {
          result.push(items[i].key)
          break;
        }
      }
    }
  }
  return result
}

// // 根据权限来过滤菜单
// export const filterMenusByAuthority = (items, checkedKeys) => {
//   const results = []
//   items.forEach((item) => {
//     // 如果有1级, 就代表内部的2级都有权限
//     if (checkedKeys.includes(item.key)) {
//       results.push(item)
//     } else {
//     // 如果没有1级,需要判断一下这个1级内部有没有某一个2级有权限
//       const newItem = { ...item, children: [] }
//       item.children?.forEach((v) => {
//         if (checkedKeys.includes(v.key)) newItem.children.push(v)
//       })
//       if (newItem.children.length) {
//         results.push(newItem)
//       }
//     }
//   })
//   return results
// }

export default menus
